<?php
namespace User\Controller;
use Think\Controller;
use Think\Image;
use Think\Page;
use Think\Upload;
use User\Model\FactoryModel;
use User\Model\MeetingModel;

class MeetingController extends UserController
{
    public function __construct()
    {
        parent::__construct();
        if (!headers_sent() && // 如果页面头部信息还没有输出
            extension_loaded("zlib") && // 而且php已经加载了zlib扩展
            strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")
        ) //而且浏览器接受GZIP
        {
            ini_set('zlib.output_compression', 'On');
            ini_set('zlib.output_compression_level', '4');
        }
    }

    public function index()
    {
        $cond['status'] = array("NEQ", MeetingModel::STATUS_DELETE);
        $cond['token'] = $this->wxuser['token'];

        $count = M("meeting")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("meeting")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('meet_id desc')->select();
        $this->assign("list", $list);
        $this->display();
    }

    public function meetingAdd()
    {
        $params = I("post.", "", "");
        if (empty($params)) {
            $type = 'add';
            $this->assign('type', $type);
            return $this->display('meetingUpdate');
        }
        if (empty($params['name'])) {
            $this->ajaxReturn(['code' => -1, 'msg' => '请填写会议名称']);
        }

        $data['name'] = $params['name'];
        if (!empty($params['start_date'])) {
            $data['start_date'] = $params['start_date'];
        }
        if (!empty($params['end_date'])) {
            $data['end_date'] = $params['end_date'];
        }
        if (!empty($params['backurl'])) {
            $result = FactoryModel::ajaxImgUploadThumb($params['backurl'], 'meeting');
            if ($result['code'] == 1) {
                $data['backurl'] = $result['msg'];
            }
        }
        if (!empty($params['bannerurl'])) {
            $result = FactoryModel::ajaxImgUploadThumb($params['bannerurl'], 'meeting');
            if ($result['code'] == 1) {
                $data['bannerurl'] = $result['msg'];
            }
        }
        $data['share_disable'] = empty($params['share_disable']) ? MeetingModel::SHARE_ABLE : MeetingModel::SHARE_DISABLE;
        $data['wx_disable'] = empty($params['wx_disable']) ? MeetingModel::WX_ABLE : MeetingModel::WX_DISABLE;
        $data['need_register'] = empty($params['need_register']) ? MeetingModel::REGISTER_NO : MeetingModel::REGISTER_YES;
        $data['status'] = MeetingModel::STATUS_PENDING;
        $data['has_head'] = empty($params['has_head']) ? MeetingModel::HEAD_NO : MeetingModel::HEAD_YES;
        if (!empty($params['has_head'])) {
            $result = FactoryModel::ajaxImgUploadThumb($params['logourl'], 'meeting');
            if ($result['code'] == 1) {
                $data['logourl'] = $result['msg'];
            }
            $data['describe'] = $params['describe'];
        }
        $data['token'] = $this->wxuser['token'];
        $data['theme_color'] = strtoupper($params['theme_color']);
        if ($params['clear_pic']) {
            $data['backurl'] = '';
        }
        $data['share_title'] = $params['name'];
        $data['share_desc'] = "欢迎参加" . $params['name'];
        $data['share_pic'] = "/Public/projects/meeting/images/bg.jpg";
        M("meeting")->add($data);
        $this->ajaxReturn(['code' => 1, 'msg' => '成功']);
    }

    public function meetingUpdate()
    {
        $type = I("get.type", "", "");
        $meetId = I("get.meetId", "", "");
        $params = I("post.", "", "");
        if ($type == 'update' && empty($params)) {
            $meet = M("meeting")->where(['meet_id' => $meetId])->find();
            $this->assign("meet", $meet);
            return $this->display();
        }
        $meetId = I("post.meetId", "", "");
        if (empty($meetId)) {
            $this->ajaxReturn(['code' => -1, 'msg' => '无会议信息']);
        }
        if (empty($params['name'])) {
            $this->ajaxReturn(['code' => -1, 'msg' => '会议名称不能为空']);
        }
        $data['name'] = $params['name'];
        if (!empty($params['start_date'])) {
            $data['start_date'] = $params['start_date'];
        }
        if (!empty($params['end_date'])) {
            $data['end_date'] = $params['end_date'];
        }
        if (!empty($params['backurl'])) {
            $result = FactoryModel::ajaxImgUploadThumb($params['backurl'], 'meeting');
            if ($result['code'] == 1) {
                $data['backurl'] = $result['msg'];
            }
        }
        if (!empty($params['bannerurl'])) {
            $result = FactoryModel::ajaxImgUploadThumb($params['bannerurl'], 'meeting');
            if ($result['code'] == 1) {
                $data['bannerurl'] = $result['msg'];
            }
        }
        if (!empty($params['wxcode'])) {
            $result = FactoryModel::ajaxImgUploadThumb($params['wxcode'], 'meeting');
            if ($result['code'] == 1) {
                $data['wxcode'] = $result['msg'];
            }
        }
        $data['share_disable'] = empty($params['share_disable']) ? MeetingModel::SHARE_ABLE : MeetingModel::SHARE_DISABLE;
        $data['wx_disable'] = empty($params['wx_disable']) ? MeetingModel::WX_ABLE : MeetingModel::WX_DISABLE;
        $data['need_register'] = empty($params['need_register']) ? MeetingModel::REGISTER_NO : MeetingModel::REGISTER_YES;
        $data['has_head'] = empty($params['has_head']) ? MeetingModel::HEAD_NO : MeetingModel::HEAD_YES;
        if (!empty($params['has_head'])) {
            $result = FactoryModel::ajaxImgUploadThumb($params['logourl'], 'meeting');
            if ($result['code'] == 1) {
                $data['logourl'] = $result['msg'];
            }
            $data['describe'] = $params['describe'];
        }
        $data['theme_color'] = $params['theme_color'];
        if ($params['clear_pic']) {
            $data['backurl'] = '';
        }
        M("meeting")->where(['meet_id' => $meetId])->save($data);
        $this->ajaxReturn(['code' => 1, 'msg' => '成功']);
    }

    public function meetingActive()
    {
        $meetId = I("get.meetId", "", "");
        $data['status'] = MeetingModel::STATUS_ACTIVE;
        if (!empty($meetId)) {
            M("meeting")->where(['meet_id' => $meetId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function meetingDelete()
    {
        $meetId = I("get.meetId", "", "");
        $data['status'] = MeetingModel::STATUS_DELETE;
        if (!empty($meetId)) {
            M("meeting")->where(['meet_id' => $meetId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function meetingPend()
    {
        $meetId = I("get.meetId", "", "");
        $data['status'] = MeetingModel::STATUS_PENDING;
        if (!empty($meetId)) {
            M("meeting")->where(['meet_id' => $meetId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function city()
    {
        $cond_city['parentid'] = I("get.province", "", "");
        if (!empty($cond_city['parentid']) && $cond_city['parentid'] != '-') {
            $city = M("region")->where($cond_city)->select();
            echo json_encode($city);
        } else {
            echo "";
        }
    }

    public function district()
    {
        $cond_district['parentid'] = I("get.city", "", "");
        if (!empty($cond_district['parentid']) && $cond_district['parentid'] != '-') {
            $district = M("region")->where($cond_district)->select();
            echo json_encode($district);
        } else {
            echo "";
        }
    }

    public function menu()
    {
        $cond['meet_id'] = I('get.meetId', -1, '');
        $cond['status'] = array("NEQ", MeetingModel::STATUS_DELETE);
        $list = M("meeting_menu")->where($cond)->order('priority asc')->select();
        $this->assign('list', $list);
        $menu_id = I('get.menu_id', 0, '');
        if (!empty($list) && empty($menu_id)) {
            $menu_id = $list[0]['menu_id'];
        }
        $this->assign('menu_id', $menu_id);
        $this->display();
    }

    public function ajaxMenu()
    {
        $cond['meet_id'] = I('get.meetId', -1, '');
        $cond['status'] = array("NEQ", MeetingModel::STATUS_DELETE);
        $list = M("meeting_menu")->where($cond)->order('priority asc')->select();
        $menu = '';
        if (!empty($list)) {
            $menuId = I('get.menuId', -1, '');
            if ($menuId == 0) {
                $menu = $list[0];
            } else {
                foreach ($list as $key => $val) {
                    if ($val['menu_id'] == $menuId) {
                        $menu = $list[$key];
                    }
                }
            }
        }
        $this->ajaxReturn($menu);
    }

    public function menuUpdate()
    {
        $params = I("post.", "", "");
        if (empty($params['name'])) {
            $this->ajaxReturn(['code' => -1, 'msg' => '请填写菜单名称']);
        }
        $data['name'] = $params['name'];
        if (!empty($params['backurl'])) {
            $result = FactoryModel::ajaxImgUploadThumb($params['backurl'], 'meeting');
            if ($result['code'] == 1) {
                $data['backurl'] = $result['msg'];
            }
        }
        if (!empty($params['bannerurl'])) {
            $result = FactoryModel::ajaxImgUploadThumb($params['bannerurl'], 'meeting');
            if ($result['code'] == 1) {
                $data['bannerurl'] = $result['msg'];
            }
        }

        $data['type'] = $params['type'];
        $data['link'] = $params['link'];
        $data['content'] = $params['content'];
        if (!empty($params['picurl'])) {
            $result = FactoryModel::ajaxImgUploadThumb($params['picurl'], 'meeting');
            if ($result['code'] == 1) {
                $data['picurl'] = $result['msg'];
            }
        }
        $data['meet_id'] = $cond['meet_id'] = $params['meet_id'];
        $cond['status'] = MeetingModel::STATUS_ACTIVE;
        $max_priority = M("meeting_menu")->where($cond)->max('priority');
        $data['priority'] = $max_priority + 1;
        if (empty($params['menuId'])) {
            $data['status'] = MeetingModel::STATUS_ACTIVE;
            $menu_id = M("meeting_menu")->add($data);
            $this->ajaxReturn(['code' => 2, 'msg' => '成功', 'menu_id' => $menu_id]);
        } else {
            $cond_menu['menu_id'] = $params['menuId'];
            unset($data['priority']);
            M("meeting_menu")->where($cond_menu)->save($data);
        }

        $this->ajaxReturn(['code' => 1, 'msg' => '成功']);
    }

    public function menuDelete()
    {
        $menuId = I("get.menuId", "", "");
        $data['status'] = MeetingModel::STATUS_DELETE;
        if (!empty($menuId)) {
            M("meeting_menu")->where(['menu_id' => $menuId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function menuPriorityInc()
    {
        $cond_menu['menu_id'] = $menuId = I("get.menuId", -1, "");
        $meetId = I("get.meetId", -1, "");
        $priority = M("meeting_menu")->where($cond_menu)->getField('priority');
        $cond['meet_id'] = $meetId;
        $cond['status'] = MeetingModel::STATUS_ACTIVE;
        $max_priority = M("meeting_menu")->where($cond)->max('priority');
        if ($priority < $max_priority) {
            $cond_change['status'] = MeetingModel::STATUS_ACTIVE;
            $cond_change['meet_id'] = $meetId;
            $cond_change['priority'] = $priority + 1;
            $data_change['priority'] = $priority;
            M("meeting_menu")->where($cond_change)->save($data_change);

            $data['priority'] = $priority + 1;
            M("meeting_menu")->where(['menu_id' => $menuId])->save($data);
            return $this->my_success("成功");
        } else {
            return $this->my_error("失败");
        }

    }

    public function menuPriorityDec()
    {
        $cond_menu['menu_id'] = $menuId = I("get.menuId", -1, "");
        $meetId = I("get.meetId", -1, "");
        $priority = M("meeting_menu")->where($cond_menu)->getField('priority');;
        $cond['meet_id'] = $meetId;
        $cond['status'] = MeetingModel::STATUS_ACTIVE;
        $min_priority = M("meeting_menu")->where($cond)->min('priority');
        if ($priority > $min_priority) {
            $cond_change['status'] = MeetingModel::STATUS_ACTIVE;
            $cond_change['meet_id'] = $meetId;
            $cond_change['priority'] = $priority - 1;
            $data_change['priority'] = $priority;
            M("meeting_menu")->where($cond_change)->save($data_change);

            $data['priority'] = $priority - 1;
            M("meeting_menu")->where(['menu_id' => $menuId])->save($data);
            return $this->my_success("成功");
        } else {
            return $this->my_error("失败");
        }
    }

    public function getMenuContent()
    {
        $cond['menu_id'] = I("get.menuId", -1, "");
        $cond['type'] = I("get.type", -1, "");
        $cond['status'] = MeetingModel::STATUS_ACTIVE;
        $content = M("meeting_menu_content")->where($cond)->order('priority asc')->select();
        $this->ajaxReturn(['content' => $content]);
    }

    public function getMenuContentByid()
    {
        $cond['content_id'] = I("get.contentId", -1, '');
        $cond['status'] = MeetingModel::STATUS_ACTIVE;
        $content = M("meeting_menu_content")->where($cond)->find();
        $this->ajaxReturn($content);
    }

    public function menuContentUpdate()
    {
        $params = I("post.", "", "");
        $data['menu_id'] = $cond['menu_id'] = $params['menu_id'];
        $cond['type'] = $data['type'] = $params['type'];
        $cond['status'] = MeetingModel::STATUS_ACTIVE;
        $max_priority = M("meeting_menu_content")->where($cond)->max('priority');

        $data['title'] = empty($params['title']) ? '' : $params['title'];
        $data['describe'] = empty($params['describe']) ? '' : $params['describe'];
        $data['content'] = empty($params['content']) ? '' : $params['content'];
        if (!empty($params['picurl'])) {
            $result = FactoryModel::ajaxImgUploadThumb($params['picurl'], 'meeting');
            if ($result['code'] == 1) {
                $data['picurl'] = $result['msg'];
            }
        }
        $data['link'] = $params['link'];
        $contentId = $params['contentId'];
        if (empty($contentId)) {
            $data['priority'] = $max_priority + 1;
            $data['status'] = MeetingModel::STATUS_ACTIVE;
            M("meeting_menu_content")->add($data);
        } else {
            M("meeting_menu_content")->where(['content_id' => $contentId])->save($data);
        }

        $this->ajaxReturn(['code' => 1, 'msg' => '成功']);
    }

    public function menuContentDelete()
    {
        $contentId = I("get.contentId", "", "");
        $data['status'] = MeetingModel::STATUS_DELETE;
        if (!empty($contentId)) {
            M("meeting_menu_content")->where(['content_id' => $contentId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function menuContentPriorityInc()
    {
        $menuId = I("get.menuId", -1, "");
        $cond_content['content_id'] = $contentId = I("get.contentId", -1, "");
        $type = I("get.type", -1, "");
        $cond['menu_id'] = $menuId;
        $cond['status'] = MeetingModel::STATUS_ACTIVE;
        $cond_content['type'] = $cond['type'] = $type;
        $priority = M("meeting_menu_content")->where($cond_content)->getField('priority');
        $max_priority = M("meeting_menu_content")->where($cond)->max('priority');
        if ($priority < $max_priority) {
            $cond_change['menu_id'] = $menuId;
            $cond_change['status'] = MeetingModel::STATUS_ACTIVE;
            $cond_change['priority'] = $priority + 1;
            $cond_change['type'] = $type;
            $data_change['priority'] = $priority;
            M("meeting_menu_content")->where($cond_change)->save($data_change);

            $data['priority'] = $priority + 1;
            M("meeting_menu_content")->where(['content_id' => $contentId])->save($data);
            return $this->my_success("成功");
        } else {
            return $this->my_error("失败");
        }
    }

    public function menuContentPriorityDec()
    {
        $menuId = I("get.menuId", -1, "");
        $cond_content['content_id'] = $contentId = I("get.contentId", -1, "");
        $type = I("get.type", -1, "");
        $cond['menu_id'] = $menuId;
        $cond['status'] = MeetingModel::STATUS_ACTIVE;
        $cond_content['type'] = $cond['type'] = $type;
        $priority = M("meeting_menu_content")->where($cond_content)->getField('priority');
        $min_priority = M("meeting_menu_content")->where($cond)->min('priority');
        if ($priority > $min_priority) {
            $cond_change['menu_id'] = $menuId;
            $cond_change['status'] = MeetingModel::STATUS_ACTIVE;
            $cond_change['priority'] = $priority - 1;
            $cond_change['type'] = $type;
            $data_change['priority'] = $priority;
            M("meeting_menu_content")->where($cond_change)->save($data_change);

            $data['priority'] = $priority - 1;
            M("meeting_menu_content")->where(['content_id' => $contentId])->save($data);
            return $this->my_success($min_priority);
        } else {
            return $this->my_error("失败");
        }
    }

    public function registerField()
    {
        $cond['meet_id'] = I('get.meetId', -1, '');
        $cond['status'] = array("NEQ", MeetingModel::STATUS_DELETE);
        $list = M("meeting_register_field")->where($cond)->order('priority asc')->select();
        $meet = M("meeting")->where($cond)->find();
        $this->assign('list', $list);
        $this->assign('meet', $meet);
        $this->display();
    }

    public function fieldUpdate()
    {
        $params = I('get.', '', '');
        $data['meet_id'] = empty($params['meetId']) ? 0 : $params['meetId'];
        $data['name'] = empty($params['name']) ? '' : $params['name'];
        $data['type'] = empty($params['type']) ? 0 : $params['type'];
        $data['format'] = empty($params['format']) ? 0 : $params['format'];
        $data['is_require'] = empty($params['is_require']) ? 0 : $params['is_require'];
        $data['is_condition'] = empty($params['is_condition']) ? 0 : $params['is_condition'];
        $data['option'] = empty($params['option']) ? '' : json_encode($params['option']);
        $data['min_value'] = empty($params['min_value']) ? 0 : $params['min_value'];
        $data['max_value'] = empty($params['max_value']) ? 0 : $params['max_value'];
        $field_change = M("meeting_register_field");
        $field_change->startTrans();
        if (empty($params['fieldId'])) {
            $data['status'] = MeetingModel::STATUS_ACTIVE;
            $cond['meet_id'] = $data['meet_id'];
            $cond['status'] = MeetingModel::STATUS_ACTIVE;
            $priority = M("meeting_register_field")->where($cond)->max('priority');
            $data['priority'] = $priority + 1;
            M("meeting_register_field")->add($data);
        } else {
            $cond['field_id'] = $params['fieldId'];
            M("meeting_register_field")->where($cond)->save($data);
        }
        $cond_result['meet_id'] = $data['meet_id'];
        $cond_result['status'] = MeetingModel::STATUS_ACTIVE;
        $cond_result['is_condition'] = MeetingModel::CONDITION_YES;
        $count = M("meeting_register_field")->where($cond_result)->count();
        if ($count <= 3) {
            $field_change->commit();
            $this->my_success("提交成功");
        } else {
            $field_change->rollback();
            $this->my_error("查询字段数量不能超过3个");
        }
    }

    public function menuFieldUpdate()
    {
        $cond['meet_id'] = $data['meet_id'] = I("post.meetId", '', '');
        if ($data['meet_id']) {
            $data['register_start_date'] = I("post.register_start_date", '', '');
            $data['register_end_date'] = I("post.register_end_date", '', '');
            $data['need_ticket'] = I("post.need_ticket", 0, '');
            $data['ticket_desc'] = I("post.ticket_desc", 0, '');
            $data['need_verify'] = I("post.need_verify", 0, '');
            $data['bus_service'] = I("post.bus_service", 0, '');
            $data['hotel_service'] = I("post.hotel_service", 0, '');
            $data['eat_service'] = I("post.eat_service", 0, '');
            $data['register_limit'] = I("post.register_limit", 0, '');
            $data['result_type'] = I("post.result_type", 0, '');
            $data['result_msg'] = I("post.result_msg", '', '');
            M("meeting")->where($cond)->save($data);
        }
        $this->my_success("成功");
    }

    public function getField()
    {
        $cond['field_id'] = I("get.fieldId", "", "");
        $field = M("meeting_register_field")->where($cond)->find();
        return $this->my_success($field);
    }

    public function fieldDelete()
    {
        $fieldId = I("get.fieldId", "", "");
        $data['status'] = MeetingModel::STATUS_DELETE;
        if (!empty($fieldId)) {
            M("meeting_register_field")->where(['field_id' => $fieldId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function fieldPriorityInc()
    {
        $cond['meet_id'] = $meetId = I("get.meetId", -1, "");
        $cond['status'] = MeetingModel::STATUS_ACTIVE;
        $cond_field['field_id'] = $fieldId = I("get.fieldId", -1, "");
        $priority = M("meeting_register_field")->where($cond_field)->getField('priority');
        $max_priority = M("meeting_register_field")->where($cond)->max('priority');
        if ($priority < $max_priority) {
            $cond_change['meet_id'] = $meetId;
            $cond_change['status'] = MeetingModel::STATUS_ACTIVE;
            $cond_change['priority'] = $priority + 1;

            $data_change['priority'] = $priority;
            M("meeting_register_field")->where($cond_change)->save($data_change);

            $data['priority'] = $priority + 1;
            M("meeting_register_field")->where($cond_field)->save($data);
            return $this->my_success("成功");
        } else {
            return $this->my_error("失败");
        }
    }

    public function fieldPriorityDec()
    {
        $cond['meet_id'] = $meetId = I("get.meetId", -1, "");
        $cond['status'] = MeetingModel::STATUS_ACTIVE;
        $cond_field['field_id'] = $fieldId = I("get.fieldId", -1, "");
        $priority = M("meeting_register_field")->where($cond_field)->getField('priority');
        $min_priority = M("meeting_register_field")->where($cond)->min('priority');
        if ($priority > $min_priority) {
            $cond_change['meet_id'] = $meetId;
            $cond_change['status'] = MeetingModel::STATUS_ACTIVE;
            $cond_change['priority'] = $priority - 1;

            $data_change['priority'] = $priority;
            M("meeting_register_field")->where($cond_change)->save($data_change);

            $data['priority'] = $priority - 1;
            M("meeting_register_field")->where($cond_field)->save($data);
            return $this->my_success("成功");
        } else {
            return $this->my_error("失败");
        }
    }

    public function register()
    {
        $cond['meet_id'] = I('get.meetId', -1, '');
        $cond['status'] = array("NEQ", MeetingModel::STATUS_DELETE);

        //--会议信息--
        $meet = M("meeting")->where($cond)->find();
        $this->assign('meet', $meet);

        //--字段名称--
        $fields = M("meeting_register_field")->where($cond)->order('priority asc')->select();
        $this->assign('fields', $fields);
        //--条件--
        $conditions = array();
        foreach ($fields as $field) {
            if ($field['is_condition']) {
                $conditions[] = $field;
            }
        }
        $this->assign('conditions', $conditions);
        //--表格宽度--
        $count = sizeof($fields);
        $this->assign('count', ($count + 1) * 100 + 200);

        $query_value1 = I('get.query_value1', '', '');
        if ($query_value1 !== '') {
            $cond['query_value1'] = $query_value1;
        }
        $query_value2 = I('get.query_value2', '', '');
        if ($query_value2 !== '') {
            $cond['query_value2'] = $query_value2;
        }
        $query_value3 = I('get.query_value3', '', '');
        if ($query_value3 !== '') {
            $cond['query_value3'] = $query_value3;
        }
        $status = I('get.status', -1, '');
        if ($status != -1) {
            $cond['status'] = $status;
        }
        $count = M("meeting_user")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("meeting_user")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('user_id desc')->select();
        $this->assign("list", $list);
        $this->display();
    }

    public function registerDetail()
    {
        $cond['meet_id'] = I('get.meetId', -1, '');
        $cond['status'] = array("NEQ", MeetingModel::STATUS_DELETE);

        $meet = M("meeting")->where($cond)->find();
        $this->assign('meet', $meet);
        $fields = M("meeting_register_field")->where($cond)->order('priority asc')->select();
        $this->assign('fields', $fields);

        $cond_user['user_id'] = I('get.userId', -1, '');
        $user = M("meeting_user")->where($cond_user)->find();
        $this->assign('user', $user);

        $field_values = explode(";", $user['field_value']);
        $display_field = array();
        foreach ($field_values as $field_value) {
            $echo_values = explode("|", $field_value);
            $types = explode(",", $echo_values[0]);
            $display_field[$types[0]] = $echo_values[1];
        }
        $this->assign('display_field', $display_field);
        $this->display();
    }

    public function registerUpdate()
    {
        $params = I("post.", "", "");
        if (empty($params['meetId'])) {
            return $this->redirect('/User/Meeting/index');
        }
        $cond['meet_id'] = $params['meetId'];
        $cond['status'] = array("NEQ", MeetingModel::STATUS_DELETE);
        $fields = M("meeting_register_field")->where($cond)->order('priority asc')->select();
        $data['meet_id'] = $params['meetId'];
        $img = $this->upload();
        $querynum = 1;
        foreach ($fields as $field) {
            if ($field['is_condition']) {
                if (isset($params[$field['field_id']])) {
                    $data['query_value' . $querynum] = $params[$field['field_id']];
                }
                $querynum++;
            }
            if ($field['type'] == MeetingModel::FIELD_MULTIPLE) {
                if (isset($params[$field['field_id']])) {
                    $data['field_value'] .= $field['field_id'] . "," . MeetingModel::FIELD_MULTIPLE . "|" . json_encode($params[$field['field_id']]) . ";";
                } else {
                    $data['field_value'] .= $field['field_id'] . "," . MeetingModel::FIELD_MULTIPLE . "|;";
                }
            } else {
                if (array_key_exists($field['field_id'], $img)) {
                    $data['field_value'] .= $field['field_id'] . "," . MeetingModel::FIELD_PIC . "|" . $img[$field['field_id']] . ";";
                    continue;
                }
                if ($field['type'] == MeetingModel::FIELD_CITY) {
                    $data['field_value'] .= $field['field_id'] . "|" . $params['province' . $field['field_id']] . "," .
                        $params['city' . $field['field_id']] . "," . $params['distinct' . $field['field_id']] . ";";
                    continue;
                }
                if ($field['type'] == MeetingModel::FIELD_ADDRESS) {
                    $data['field_value'] .= $field['field_id'] . "|" . $params['province' . $field['field_id']] . "," .
                        $params['city' . $field['field_id']] . "," . $params['distinct' . $field['field_id']] .
                        "," . $params['address' . $field['field_id']] . ";";
                    continue;
                }
                if ($field['type'] == MeetingModel::FIELD_RANGE) {
                    $data['field_value'] .= $field['field_id'] . "|" . $params['start' . $field['field_id']] . "-" . $params['end' . $field['field_id']] . ";";
                    continue;
                }
                if ($field['type'] == MeetingModel::FIELD_PIC) {
                    if (isset($params[$field['field_id']])) {
                        $data['field_value'] .= $field['field_id'] . "," . MeetingModel::FIELD_PIC . "|" . $params[$field['field_id']] . ";";
                    } else {
                        $data['field_value'] .= $field['field_id'] . "," . MeetingModel::FIELD_PIC . "|;";
                    }
                    continue;
                }
                if (isset($params[$field['field_id']])) {
                    $data['field_value'] .= $field['field_id'] . "|" . $params[$field['field_id']] . ";";
                } else {
                    $data['field_value'] .= $field['field_id'] . "|;";
                }
            }
        }
        $data['is_bus'] = empty($params['is_bus']) ? 0 : $params['is_bus'];
        $data['bus_person'] = empty($params['bus_person']) ? 0 : $params['bus_person'];
        $data['bus_note'] = empty($params['bus_note']) ? '' : $params['bus_note'];

        $data['is_eat'] = empty($params['is_eat']) ? 0 : $params['is_eat'];
        $data['eat_person'] = empty($params['eat_person']) ? 0 : $params['eat_person'];
        $data['eat_note'] = empty($params['eat_note']) ? '' : $params['eat_note'];

        $data['is_hotel'] = empty($params['is_hotel']) ? 0 : $params['is_hotel'];
        $data['hotel_person'] = empty($params['hotel_person']) ? 0 : $params['hotel_person'];
        $data['hotel_note'] = empty($params['hotel_note']) ? '' : $params['hotel_note'];
        $data['result'] = empty($params['result']) ? '' : $params['result'];
        if (empty($params['userId'])) {
            $data['status'] = MeetingModel::STATUS_ACTIVE;
            $data['ticket'] = date("mdi") . rand(1, 100) . substr(md5(date("Y-m-d H:i:s")), 0, 5);
            M("meeting_user")->add($data);
        } else {
            $data['status'] = $params['status'];
            $cond_save['user_id'] = $params['userId'];
            M("meeting_user")->where($cond_save)->save($data);
        }
        return $this->redirect("/User/Meeting/register?meetId=" . $params['meetId']);
    }

    public function registerDelete()
    {
        $userId = I("get.userId", "", "");
        $data['status'] = MeetingModel::STATUS_DELETE;
        if (!empty($userId)) {
            M("meeting_user")->where(['user_id' => $userId])->save($data);
        }
        return $this->my_success("成功");
    }

    private function upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Uploads/meeting/'; // 设置附件上传根目录
        $upload->subName = array("date", "Ymd");
        $upload->saveName = array("uniqid", "meeting");
        // 上传文件
        $info = $upload->upload();
        $img = array();
        if ($info) {
            foreach ($info as $key => $file) {
                $image = new Image();
                $image->open("Public/Uploads/meeting/" . date('Ymd') . "/" . $file['savename']);//生成缩略图
                $image->thumb(102, 142, Image::IMAGE_THUMB_FIXED)->save("Public/Uploads/meeting/" . date('Ymd') . "/thumb" . $file['savename']);
                $img[$key] = "http://" . $_SERVER["HTTP_HOST"] . '/Public/Uploads/meeting/' . date('Ymd') . "/thumb" . $file['savename'];
            }
        }
        return $img;
    }


    public function share()
    {
        $params = I('post.', '', '');
        if (empty($params)) {
            $id = I('get.id', '', '');
            if (empty($id)) {
                return $this->redirect('/User/Meeting/index');
            }
            $meet = M("meeting")->where(['meet_id' => $id])->find();
            if (empty($meet)) {
                return $this->redirect('/User/Meeting/index');
            }
            $this->assign('meet', $meet);
            return $this->display();
        } else {
            if (!empty($params['id'])) {
                $data_info['share_title'] = $params['share_title'];
                $data_info['share_desc'] = $params['share_desc'];
                $data_info['share_url'] = $params['share_url'];
                $img = $params['share_pic'];
                if (!empty($img)) {
                    $ajaxImg = FactoryModel::ajaxImgUploadThumb($img, "meeting");
                    if ($ajaxImg['code'] != 1) {
                        return $this->my_error('图片上传失败');
                    }
                    $data_info['share_pic'] = $ajaxImg['msg'];
                }
                M("meeting")->where(['meet_id' => $params['id']])->save($data_info);
            }
            return $this->my_success("成功");
        }
    }

    public function statistics()
    {
        $params = I("get.", "", "");
        $cond['topic_id'] = $params['meetId'];
        $date = array("start_date" => "", "end_date" => "");

        if (empty($params['start_date'])) {
            $date['start_date'] = date("Y-m-d", strtotime("-7 days"));
        } else {
            $date['start_date'] = $params['start_date'];
        }

        if (empty($params['end_date'])) {
            $date['end_date'] = date("Y-m-d");
        } else {
            $date['end_date'] = $params['end_date'];
        }
        $cond['date'] = array("BETWEEN", array($date['start_date'], $date['end_date']));
        $cond['api'] = "meeting";
        $statistics = M("statistics")->where($cond)->getField('date, pv_num, uv_num');
        $statistics_dates = [];
        $statistics_pv = [];
        $statistics_uv = [];
        foreach ($statistics as $val) {
            $statistics_dates[] = $val['date'];
            $statistics_pv[] = $val['pv_num'];
            $statistics_uv[] = $val['uv_num'];
        }
        if (date("Y-m-d") >= $date['start_date'] && date("Y-m-d") <= $date['end_date']) {
            if ($statistics_dates && ($statistics_dates[sizeof($statistics_dates) - 1] == date("Y-m-d"))) {
                $meeting_data = S("meeting");
                if (isset($meeting_data[$params['meetId']])) {
                    $statistics_pv[sizeof($statistics_dates) - 1] = $statistics_pv[sizeof($statistics_dates) - 1] + $meeting_data[$params['meetId']]['pv'];
                    $statistics_uv[sizeof($statistics_dates) - 1] = $statistics_uv[sizeof($statistics_dates) - 1] + $meeting_data[$params['meetId']]['uv'];
                }
            } else {
                $today_pv = 0;
                $today_uv = 0;
                $meeting_data = S("meeting");
                if (isset($meeting_data[$params['meetId']])) {
                    $today_pv = $meeting_data[$params['meetId']]['pv'];
                    $today_uv = $meeting_data[$params['meetId']]['uv'];
                }
                $statistics_dates[] = date("Y-m-d");
                $statistics_pv[] = $today_pv;
                $statistics_uv[] = $today_uv;
            }
        }
        $this->assign('dates', json_encode($statistics_dates));
        $this->assign('pv', json_encode($statistics_pv));
        $this->assign('uv', json_encode($statistics_uv));

        $cond_total['status'] = array("NEQ", MeetingModel::STATUS_DELETE);
        $cond_total['meet_id'] = $params['meetId'];
        $total = M("meeting_user")->where($cond_total)->count();

        $cond_today = $cond_total;
        $cond_today['create_date'] = array("BETWEEN", array(date("Y-m-d") . " 00:00:00", date("Y-m-d") . " 23:59:59"));
        $today = M("meeting_user")->where($cond_today)->count();

        $cond_unsign['meet_id'] = $params['meetId'];
        $cond_unsign['status'] = MeetingModel::STATUS_ACTIVE;
        $unsign = M("meeting_user")->where($cond_unsign)->count();

        $cond_signed['meet_id'] = $params['meetId'];
        $cond_signed['status'] = MeetingModel::STATUS_USED;
        $signed = M("meeting_user")->where($cond_signed)->count();

        $this->assign('total', $total);
        $this->assign('today', $today);
        $this->assign('sign', json_encode(array($unsign, $signed)));
        $this->display();
    }

    public function download()
    {
        set_time_limit(0);
        $id = I("get.id", "", "");
        if (empty($id)) {
            $this->redirect("/User/Meeting/index");
        }
        $cond_field['meet_id'] = $id;
        $cond_field['status'] = MeetingModel::STATUS_ACTIVE;
        $fields = M("meeting_register_field")->where($cond_field)->order('priority asc')->select();
        vendor('phpexcel.PHPExcel');
        $objExcel = new \PHPExcel();
        $objActSheet = $objExcel->getActiveSheet();
        $title_col = 0;
        foreach ($fields as $key => $field) {
            $col = chr(65 + $title_col);
            $objActSheet->getColumnDimension($col)->setWidth(30);
            $objActSheet->setCellValue($col . "1", $field['name']);
            $title_col++;
        }

        $col = chr(65 + $title_col);
        $objActSheet->getColumnDimension($col)->setWidth(30);
        $objActSheet->setCellValue($col . "1", "是否餐饮");
        $title_col++;
        $col = chr(65 + $title_col);
        $objActSheet->getColumnDimension($col)->setWidth(30);
        $objActSheet->setCellValue($col . "1", "餐饮人数");
        $title_col++;
        $col = chr(65 + $title_col);
        $objActSheet->getColumnDimension($col)->setWidth(30);
        $objActSheet->setCellValue($col . "1", "餐饮备注");
        $title_col++;

        $col = chr(65 + $title_col);
        $objActSheet->getColumnDimension($col)->setWidth(30);
        $objActSheet->setCellValue($col . "1", "是否乘车");
        $title_col++;
        $col = chr(65 + $title_col);
        $objActSheet->getColumnDimension($col)->setWidth(30);
        $objActSheet->setCellValue($col . "1", "乘车人数");
        $title_col++;
        $col = chr(65 + $title_col);
        $objActSheet->getColumnDimension($col)->setWidth(30);
        $objActSheet->setCellValue($col . "1", "乘车备注");
        $title_col++;

        $col = chr(65 + $title_col);
        $objActSheet->getColumnDimension($col)->setWidth(30);
        $objActSheet->setCellValue($col . "1", "是否住宿");
        $title_col++;
        $col = chr(65 + $title_col);
        $objActSheet->getColumnDimension($col)->setWidth(30);
        $objActSheet->setCellValue($col . "1", "住宿人数");
        $title_col++;
        $col = chr(65 + $title_col);
        $objActSheet->getColumnDimension($col)->setWidth(30);
        $objActSheet->setCellValue($col . "1", "住宿备注");

        $title_col++;
        $col = chr(65 + $title_col);
        $objActSheet->getColumnDimension($col)->setWidth(30);
        $objActSheet->setCellValue($col . "1", "签到时间");

        $title_col++;
        $col = chr(65 + $title_col);
        $objActSheet->getColumnDimension($col)->setWidth(30);
        $objActSheet->setCellValue($col . "1", "状态");

        $cond_user['meet_id'] = $id;
        $cond_user['status'] = array("NEQ", MeetingModel::STATUS_DELETE);
        $query_value1 = I('get.query_value1', '', '');
        if ($query_value1 !== '') {
            $cond_user['query_value1'] = $query_value1;
        }
        $query_value2 = I('get.query_value2', '', '');
        if ($query_value2 !== '') {
            $cond_user['query_value2'] = $query_value2;
        }
        $query_value3 = I('get.query_value3', '', '');
        if ($query_value3 !== '') {
            $cond_user['query_value3'] = $query_value3;
        }
        $status = I('get.status', -1, '');
        if ($status != -1) {
            $cond_user['status'] = $status;
        }
        $line = 2;
        $count = M("meeting_user")->where($cond_user)->count();
        $number = $count / 100;
        for ($i = 0; $i <= $number; $i++) {
            $list = M("meeting_user")->where($cond_user)->limit($i * 100, 100)->order('user_id desc')->select();
            if (!empty($list)) {
                foreach ($list as $value) {
                    $user_value = $value['field_value'];
                    $user_value_arr = explode(";", $user_value);
                    $export_user_arr = [];
                    foreach ($user_value_arr as $user_value_arr_value) {
                        $user_value_arr_value_arr = explode("|", $user_value_arr_value);
                        if (!strpos($user_value_arr_value_arr[0], ",")) {
                            $export_user_arr[$user_value_arr_value_arr[0]] = $user_value_arr_value_arr[1];
                        } else {
                            $key_type = explode(",", $user_value_arr_value_arr[0]);
                            $export_user_arr[$key_type[0]] = $user_value_arr_value_arr[1];
                        }
                    }
                    foreach ($fields as $key_field => $field) {
                        $col = chr(65 + $key_field);
                        if (array_key_exists($field['field_id'], $export_user_arr)) {
                            switch ($field['type']) {
                                case MeetingModel::FIELD_MULTIPLE:
                                    $field_value_arr = json_decode($export_user_arr[$field['field_id']]);
                                    if (is_array($field_value_arr)) {
                                        $field_value_arr = implode(",", $field_value_arr);
                                    }
                                    $objActSheet->setCellValue($col . $line, $field_value_arr);
                                    break;
                                case MeetingModel::FIELD_PIC:
                                    $img = @imagecreatefromjpeg(C("SITE_URL") . $export_user_arr[$field['field_id']]);
                                    if ($img) {
                                        $objDrawing = new \PHPExcel_Worksheet_MemoryDrawing();
                                        $objDrawing->setImageResource($img);
                                        $objDrawing->setHeight(120);
                                        $objDrawing->setWidth(120);
                                        $objDrawing->setCoordinates($col . $line);
                                        $objDrawing->setOffsetX(10);
                                        $objDrawing->setRenderingFunction(\PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT);
                                        $objDrawing->setMimeType(\PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
                                        $objDrawing->setWorksheet($objActSheet);
                                    } else {
                                        $objActSheet->setCellValue($col . $line, '');
                                    }
                                    break;
                                default:
                                    $objActSheet->setCellValue($col . $line, $export_user_arr[$field['field_id']]);
                                    break;
                            }
                        } else {
                            $objActSheet->setCellValue($col . $line, '');
                        }
                    }
                    $col = chr(65 + sizeof($fields));
                    $field_value = $value['is_eat'] ? "是" : "否";
                    $objActSheet->setCellValue($col . $line, $field_value);
                    $col = chr(65 + sizeof($fields) + 1);
                    $objActSheet->setCellValue($col . $line, $value['eat_person']);
                    $col = chr(65 + sizeof($fields) + 2);
                    $objActSheet->setCellValue($col . $line, $value['eat_note']);
                    $col = chr(65 + sizeof($fields) + 3);
                    $field_value = $value['is_bus'] ? "是" : "否";
                    $objActSheet->setCellValue($col . $line, $field_value);
                    $col = chr(65 + sizeof($fields) + 4);
                    $objActSheet->setCellValue($col . $line, $value['bus_person']);
                    $col = chr(65 + sizeof($fields) + 5);
                    $objActSheet->setCellValue($col . $line, $value['bus_note']);
                    $col = chr(65 + sizeof($fields) + 6);
                    $field_value = $value['is_hotel'] ? "是" : "否";
                    $objActSheet->setCellValue($col . $line, $field_value);
                    $col = chr(65 + sizeof($fields) + 7);
                    $objActSheet->setCellValue($col . $line, $value['hotel_person']);
                    $col = chr(65 + sizeof($fields) + 8);
                    $objActSheet->setCellValue($col . $line, $value['hotel_note']);
                    $col = chr(65 + sizeof($fields) + 9);
                    $objActSheet->setCellValue($col . $line, $value['ticket_date']);
                    $col = chr(65 + sizeof($fields) + 10);
                    $objActSheet->setCellValue($col . $line, MeetingModel::getStatus($value['status']));
                    $objActSheet->getRowDimension($line)->setRowHeight(120);
                    $line++;
                }
            }
        }
        $fileName = '会议报名' . date('Ymd') . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = new \PHPExcel_Writer_Excel2007($objExcel);
        $objWriter->save('php://output');
    }

    public function live()
    {
        $cond['meet_id'] = I("get.meetId", -1, "");
        $meet = M("meeting")->where($cond)->find();
        $live = explode("|", $meet['live']);
        $this->assign("live", $live);
        return $this->display();
    }

    public function live_add()
    {
        $data['live'] = I("get.live", "", "");
        $cond['meet_id'] = I("get.meetId", -1, "");
        $live = M("meeting")->where($cond)->getField("live");
        if (!empty($live)) {
            $data['live'] = $live . "|" . $data['live'];
        }
        M("meeting")->where($cond)->save($data);
        $this->ajaxReturn("success");
    }

    public function live_delete()
    {
        $data['live'] = array(I("get.live", "", ""));
        $cond['meet_id'] = I("get.meetId", -1, "");
        $live = M("meeting")->where($cond)->getField("live");
        if (!empty($live)) {
            $live_arr = explode("|", $live);
            $live_arr_new = array_diff($live_arr, $data['live']);
            if ($live_arr_new) {
                $data['live'] = implode("|", $live_arr_new);
                M("meeting")->where($cond)->save($data);
            } else {
                $data['live'] = "";
                M("meeting")->where($cond)->save($data);
            }
        }
        $this->ajaxReturn("success");
    }

    public function lucky_index()
    {
        $meetId = I("get.meetId", -1, "");
        $cond_lucky['meet_id'] = $meetId;
        $lucky = M("meeting_lucky")->where($cond_lucky)->find();
        $this->assign("lucky", $lucky);

        $message = I("get.message", "", "");
        $this->assign("message", $message);
        return $this->display();
    }

    public function lucky_edit()
    {
        $data = array();
        $meetId = I("get.meetId", -1, "");
        $cond_lucky['meet_id'] = $meetId;
        $lucky = M("meeting_lucky")->where($cond_lucky)->find();
        $files = $_FILES;
        $message = '';
        if (empty($files)) {
            $message = "上传文件为空";
            return $this->redirect("/User/Meeting/lucky_index?meetId=" . $meetId . "&message=" . $message);
        }
        if (!empty($_FILES['back_music'])) {
            if ($_FILES['back_music']['size'] >= 10) {
                if ($_FILES['back_music']['size'] <= 2048000) {
                    $name_arr = explode(".", $_FILES['back_music']['name']);
                    $extension = $name_arr[sizeof($name_arr) - 1];
                    $array_extention_interdite = array('mp3');
                    if (in_array($extension, $array_extention_interdite)) {
                        $file_name_final = time() . mt_rand(0, 1000000) . "." . $extension;
                        $dir = "Public/Uploads/meeting/" . date('Ymd');
                        if (!is_dir($dir)) mkdir($dir); // 如果不存在则创建
                        move_uploaded_file($_FILES['back_music']['tmp_name'], $dir . "/" . $file_name_final);
                        $data['back_music'] = "/" . $dir . "/" . $file_name_final;
                    } else {
                        $message = '音频文件格式不正确';
                        return $this->redirect("/User/Meeting/lucky_index?meetId=" . $meetId . "&message=" . $message);
                    }
                } else {
                    $message = '音频文件最大尺寸不能超过2MB';
                    return $this->redirect("/User/Meeting/lucky_index?meetId=" . $meetId . "&message=" . $message);
                }
            }
        }
        if (!empty($_FILES['back_img'])) {
            if ($_FILES['back_img']['size'] >= 10) {
                if ($_FILES['back_img']['size'] <= 2048000) {
                    $name_arr = explode(".", $_FILES['back_img']['name']);
                    $extension = $name_arr[sizeof($name_arr) - 1];
                    $array_extention_interdite = array('jpg', 'gif', 'png', 'jpeg');
                    if (in_array($extension, $array_extention_interdite)) {
                        $file_name_final = time() . mt_rand(0, 1000000) . "." . $extension;
                        $dir = "Public/Uploads/meeting/" . date('Ymd');
                        if (!is_dir($dir)) mkdir($dir); // 如果不存在则创建
                        move_uploaded_file($_FILES['back_img']['tmp_name'], $dir . "/" . $file_name_final);
                        $data['back_img'] = "/" . $dir . "/" . $file_name_final;
                    } else {
                        $message = '背景图片文件格式不正确';
                        return $this->redirect("/User/Meeting/lucky_index?meetId=" . $meetId . "&message=" . $message);
                    }
                } else {
                    $message = '背景图片文件最大尺寸不能超过2MB';
                    return $this->redirect("/User/Meeting/lucky_index?meetId=" . $meetId . "&message=" . $message);
                }
            }
        }
        if (!empty($lucky)) {
            $cond['lucky_id'] = $lucky['lucky_id'];
            M("meeting_lucky")->where($cond)->save($data);
        } else {
            $data['meet_id'] = $meetId;
            M("meeting_lucky")->add($data);
        }
        $message = '保存成功';
        return $this->redirect("/User/Meeting/lucky_index?meetId=" . $meetId . "&message=" . $message);
    }

    public function lucky_item()
    {
        $meetId = I("get.meetId", -1, "");
        $cond_lucky['meet_id'] = $meetId;
        $cond_lucky['status'] = MeetingModel::STATUS_ACTIVE;
        $count = M("meeting_lucky_item")->where($cond_lucky)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("meeting_lucky_item")->where($cond_lucky)->limit($page->firstRow . ',' . $page->listRows)->order('item_id desc')->select();
        $this->assign("list", $list);
        return $this->display();
    }

    public function lucky_item_edit()
    {
        $meetId = I("post.meetId", -1, "");
        $item_id = I("post.item_id", "", "");
        $data = array();
        $data['name'] = I("post.name", "", "");
        $data['product_name'] = I("post.product_name", "", "");
        $data['num'] = I("post.num", 0, 0);
        $img = I("post.pic", "", "");
        if (!empty($img)) {
            $res = FactoryModel::ajaxImgUpload($img, 'meeting');
            if ($res['code'] == 1) {
                $data['pic'] = $res['msg'];
            }
        }
        if (empty($item_id)) {
            $data['meet_id'] = $meetId;
            M("meeting_lucky_item")->add($data);
        } else {
            $cond['item_id'] = $item_id;
            M("meeting_lucky_item")->where($cond)->save($data);
        }
        $this->ajaxReturn('1');
    }

    public function lucky_item_delete()
    {
        $cond['item_id'] = I("post.item_id", "", "");
        $data['status'] = MeetingModel::STATUS_DELETE;
        M("meeting_lucky_item")->where($cond)->save($data);

        $lucky_users = M("meeting_lucky_user")->where($cond)->getField("user_id");
        if (!empty($lucky_users)) {
            $cond_delete['user_id'] = array("IN", $lucky_users);
            $data['status'] = MeetingModel::STATUS_DELETE;
            M("meeting_lucky_user")->where($cond_delete)->save($data);

            $data_user['luckyed'] = 0;
            M("meeting_user")->where($cond_delete)->save($data_user);
        }
        $this->ajaxReturn("1");
    }

    public function lucky_user()
    {
        $meetId = I("get.meetId", -1, "");
        $cond['meet_id'] = $meetId;
        $cond['status'] = MeetingModel::STATUS_ACTIVE;
        $count = M("meeting_lucky_user")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("meeting_lucky_user")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('zj_id desc')->select();
        $this->assign("list", $list);

        $user_arr = array();
        $item_arr = array();
        foreach ($list as $val) {
            $user_arr[] = $val['user_id'];
            $item_arr[] = $val['item_id'];
        }
        if (!empty($user_arr)) {
            $cond_user['user_id'] = array("IN", $user_arr);
            $user_list = M("meeting_user")->where($cond_user)->index("user_id")->getField('user_id, query_value1, query_value2');
            $this->assign("user_list", $user_list);
        }
        if (!empty($item_arr)) {
            $cond_item['item_id'] = array("IN", $item_arr);
            $item_list = M("meeting_lucky_item")->where($cond_item)->index("item_id")->getField('item_id, name');
            $this->assign("item_list", $item_list);
        }
        return $this->display();

    }

    public function lucky_user_download()
    {

        $params = I("get.", "", "");
        if (empty($params['meetId'])) {
            return $this->redirect("/User/Meeting/index");
        }
//        header("Content-type:application/octet-stream");
        header("Accept-Ranges:bytes");
        header("Content-type:application/vnd.ms-excel;charset=UTF-8");
        header("Content-Disposition:attachment;filename=" . "活动中奖名单" . date("Y-m-d") . ".xls");
        header("Pragma: no-cache");
        header("Expires: 0");
        echo "<table border='1px;'><tr>";
        $title = [
            "奖项名称","中奖用户信息","中奖时间"
        ];
        foreach ($title as $k => $v) {
            $title[$k] = iconv("UTF-8", "GB2312", $v);
            echo "<td>" . iconv("UTF-8", "GB2312", $v) . "</td>";
        }
        echo "</tr>";
//        $title = implode("\t", $title);
//        echo "$title\n";
        $cond['meet_id'] = $params['meetId'];
        $cond['status'] = array("NEQ", MeetingModel::STATUS_DELETE);
        $count = M("meeting_lucky_user")->where($cond)->count();
        $number = $count / 100;
        for ($i = 0; $i <= $number; $i++) {
            $list = M("meeting_lucky_user")->where($cond)->limit($i * 100, 100)->order('zj_id desc')->select();
            $this->exportexcel($list);
        }
        echo "</table>";
    }

    private function exportexcel($data = array())
    {
        //导出xls 开始
        if (!empty($data)) {
            foreach ($data as $key => $val) {
                $user_arr[] = $val['user_id'];
                $item_arr[] = $val['item_id'];
            }
            $user_list = array();
            $item_list = array();
            if (!empty($user_arr)) {
                $cond_user['user_id'] = array("IN", $user_arr);
                $user_list = M("meeting_user")->where($cond_user)->index("user_id")->getField('user_id, query_value1, query_value2');
            }
            if (!empty($item_arr)) {
                $cond_item['item_id'] = array("IN", $item_arr);
                $item_list = M("meeting_lucky_item")->where($cond_item)->index("item_id")->getField('item_id, name');
            }
            foreach ($data as $key => $val) {
                echo "<tr>";
                echo "<td>" . iconv("UTF-8", "GB2312", $item_list[$val['item_id']]) . "</td>";
                echo "<td>" . iconv("UTF-8", "GB2312", $user_list[$val['user_id']]['query_value1'] . " " . $user_list[$val['user_id']]['query_value2']) . "</td>";
                echo "<td>" . iconv("UTF-8", "GB2312", $val['create_time']) . "</td>";
                echo "</tr>";
            }
        }
    }

    public function luckyUserDelete()
    {
        $cond['zj_id'] = I("get.zjId", "", "");
        $lucky_user = M("meeting_lucky_user")->where($cond)->find();
        $data['status'] = MeetingModel::STATUS_DELETE;
        M("meeting_lucky_user")->where($cond)->save($data);

        $meeting_user['luckyed'] = 0;
        $cond_user['user_id'] = $lucky_user['user_id'];
        M("meeting_user")->where($cond_user)->save($meeting_user);

        $this->ajaxReturn("1");
    }

    public function sign_index()
    {
        $meetId = I("get.meetId", -1, "");
        $cond_sign['meet_id'] = $meetId;
        $sign = M("meeting_sign")->where($cond_sign)->find();
        $this->assign("sign", $sign);

        $message = I("get.message", "", "");
        $this->assign("message", $message);
        return $this->display("sign");
    }

    public function sign_edit()
    {
        $data = array();
        $meetId = I("get.meetId", -1, "");
        $cond_sign['meet_id'] = $meetId;
        $sign = M("meeting_sign")->where($cond_sign)->find();
        $files = $_FILES;
        $message = '';
        if (empty($files)) {
            $message = "上传文件为空";
            return $this->redirect("/User/Meeting/sign_index?meetId=" . $meetId . "&message=" . $message);
        }
        if (!empty($_FILES['back_video'])) {
            if ($_FILES['back_video']['size'] >= 10) {
                if ($_FILES['back_video']['size'] <= 5120000) {
                    $name_arr = explode(".", $_FILES['back_video']['name']);
                    $extension = $name_arr[sizeof($name_arr) - 1];
                    $array_extention_interdite = array('flv', 'wmv', 'rmvb', 'mp4');
                    if (in_array($extension, $array_extention_interdite)) {
                        $file_name_final = time() . mt_rand(0, 1000000) . "." . $extension;
                        $dir = "Public/Uploads/meeting/" . date('Ymd');
                        if (!is_dir($dir)) mkdir($dir); // 如果不存在则创建
                        move_uploaded_file($_FILES['back_video']['tmp_name'], $dir . "/" . $file_name_final);
                        $data['back_video'] = "/" . $dir . "/" . $file_name_final;
                    } else {
                        $message = '视频文件格式不正确';
                        return $this->redirect("/User/Meeting/sign_index?meetId=" . $meetId . "&message=" . $message);
                    }
                } else {
                    $message = '视频文件最大尺寸不能超过5MB';
                    return $this->redirect("/User/Meeting/sign_index?meetId=" . $meetId . "&message=" . $message);
                }
            }
        }
        if (!empty($_FILES['back_music'])) {
            if ($_FILES['back_music']['size'] >= 10) {
                if ($_FILES['back_music']['size'] <= 2048000) {
                    $name_arr = explode(".", $_FILES['back_music']['name']);
                    $extension = $name_arr[sizeof($name_arr) - 1];
                    $array_extention_interdite = array('mp3');
                    if (in_array($extension, $array_extention_interdite)) {
                        $file_name_final = time() . mt_rand(0, 1000000) . "." . $extension;
                        $dir = "Public/Uploads/meeting/" . date('Ymd');
                        if (!is_dir($dir)) mkdir($dir); // 如果不存在则创建
                        move_uploaded_file($_FILES['back_music']['tmp_name'], $dir . "/" . $file_name_final);
                        $data['back_music'] = "/" . $dir . "/" . $file_name_final;
                    } else {
                        $message = '音频文件格式不正确';
                        return $this->redirect("/User/Meeting/sign_index?meetId=" . $meetId . "&message=" . $message);
                    }
                } else {
                    $message = '音频文件最大尺寸不能超过2MB';
                    return $this->redirect("/User/Meeting/sign_index?meetId=" . $meetId . "&message=" . $message);
                }
            }
        }
        if (!empty($_FILES['back_img'])) {
            if ($_FILES['back_img']['size'] >= 10) {
                if ($_FILES['back_img']['size'] <= 2048000) {
                    $name_arr = explode(".", $_FILES['back_img']['name']);
                    $extension = $name_arr[sizeof($name_arr) - 1];
                    $array_extention_interdite = array('jpg', 'gif', 'png', 'jpeg');
                    if (in_array($extension, $array_extention_interdite)) {
                        $file_name_final = time() . mt_rand(0, 1000000) . "." . $extension;
                        $dir = "Public/Uploads/meeting/" . date('Ymd');
                        if (!is_dir($dir)) mkdir($dir); // 如果不存在则创建
                        move_uploaded_file($_FILES['back_img']['tmp_name'], $dir . "/" . $file_name_final);
                        $data['back_img'] = "/" . $dir . "/" . $file_name_final;
                    } else {
                        $message = '背景图片文件格式不正确';
                        return $this->redirect("/User/Meeting/sign_index?meetId=" . $meetId . "&message=" . $message);
                    }
                } else {
                    $message = '背景图片文件最大尺寸不能超过2MB';
                    return $this->redirect("/User/Meeting/sign_index?meetId=" . $meetId . "&message=" . $message);
                }
            }
        }
        if (!empty($_FILES['back_text'])) {
            if ($_FILES['back_text']['size'] >= 10) {
                if ($_FILES['back_text']['size'] <= 2048000) {
                    $name_arr = explode(".", $_FILES['back_text']['name']);
                    $extension = $name_arr[sizeof($name_arr) - 1];
                    $array_extention_interdite = array('jpg', 'gif', 'png', 'jpeg');
                    if (in_array($extension, $array_extention_interdite)) {
                        $file_name_final = time() . mt_rand(0, 1000000) . "." . $extension;
                        $dir = "Public/Uploads/meeting/" . date('Ymd');
                        if (!is_dir($dir)) mkdir($dir); // 如果不存在则创建
                        move_uploaded_file($_FILES['back_text']['tmp_name'], $dir . "/" . $file_name_final);
                        $data['back_text'] = "/" . $dir . "/" . $file_name_final;
                    } else {
                        $message = '背景文字图片文件格式不正确';
                        return $this->redirect("/User/Meeting/sign_index?meetId=" . $meetId . "&message=" . $message);
                    }
                } else {
                    $message = '背景文字图片文件最大尺寸不能超过2MB';
                    return $this->redirect("/User/Meeting/sign_index?meetId=" . $meetId . "&message=" . $message);
                }
            }
        }
        if (!empty($_FILES['end_img'])) {
            if ($_FILES['end_img']['size'] >= 10) {
                if ($_FILES['end_img']['size'] <= 2048000) {
                    $name_arr = explode(".", $_FILES['end_img']['name']);
                    $extension = $name_arr[sizeof($name_arr) - 1];
                    $array_extention_interdite = array('jpg', 'gif', 'png', 'jpeg');
                    if (in_array($extension, $array_extention_interdite)) {
                        $file_name_final = time() . mt_rand(0, 1000000) . "." . $extension;
                        $dir = "Public/Uploads/meeting/" . date('Ymd');
                        if (!is_dir($dir)) mkdir($dir); // 如果不存在则创建
                        move_uploaded_file($_FILES['end_img']['tmp_name'], $dir . "/" . $file_name_final);
                        $data['end_img'] = "/" . $dir . "/" . $file_name_final;
                    } else {
                        $message = '结束图片文件格式不正确';
                        return $this->redirect("/User/Meeting/sign_index?meetId=" . $meetId . "&message=" . $message);
                    }
                } else {
                    $message = '结束图片文件最大尺寸不能超过2MB';
                    return $this->redirect("/User/Meeting/sign_index?meetId=" . $meetId . "&message=" . $message);
                }
            }
        }
        $data['wish'] = I("post.wish", "", "");
        if (!empty($sign)) {
            $cond['sign_id'] = $sign['sign_id'];
            M("meeting_sign")->where($cond)->save($data);
        } else {
            $data['meet_id'] = $meetId;
            M("meeting_sign")->add($data);
        }
        $message = '保存成功';
        return $this->redirect("/User/Meeting/sign_index?meetId=" . $meetId . "&message=" . $message);
    }
}

?>
